package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySqlConnector extends Connector
{

    String database;
    String host;
    String username;
    String password;
    int poolSize;
    String options;
    int port;

    public MySqlConnector(String name, String host, int port, String database,
                          String options, String username, String password, int poolSize,
                          int timeoutInMs, int connectionTimeoutInSec, int idleOpenTimeInMs,
                          int selectTimeoutSec, int updateTimeoutSec)
                                                                     throws InstantiationException, IllegalAccessException,
                                                                     ClassNotFoundException, SQLException
    {
        super(name, timeoutInMs, poolSize, idleOpenTimeInMs, selectTimeoutSec,
                        updateTimeoutSec);
        this.host = host;
        this.username = username;
        this.password = password;
        this.options = options == null ? "" : "?" + options;
        this.database = database;
        this.port = port;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        DriverManager.setLoginTimeout(connectionTimeoutInSec);
        initializePool();
    }

    @Override
    protected Connection createConnection() throws SQLException
    {
        try
        {
            return DriverManager.getConnection("jdbc:mysql://" + host + ":"
                            + port + "/" + database + options, username, password);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            throw e;
        }
    }

}
